﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Jalal.Library.Data;
using Jalal.Library.Utility;

namespace BCGC.DAL
{
    public class GameData
    {
        public static DataTable GetAllGames()
        {
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_GetGameList";

            return dataMiner.GetListData(command);
        }

        public static int CreateNewGame(string gameName)
        {
            object id = null;
            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();
            command.CommandText = "CSP_CreateNewGame";
            dataMiner.AddParameter(command, "@GameName", DbType.String, 500, ParameterDirection.Input, gameName);
            dataMiner.AddParameter(command, "ReturnValue", DbType.Int32, 0, ParameterDirection.ReturnValue, null);
            dataMiner.Execute(command, out id);

            return (int)CommonUtility.FilterNull(id, typeof(int));
        }

        public static DataTable GetAllGames(int currentPageIndex, string sortOrder, int pageSize, 
            string searchTitle, out int totalData)
        {
            totalData = 0;
            object count = null;

            DataMiner dataMiner = DataFactory.GetDataMiner();
            DbCommand command = DataFactory.GetCommand();

            dataMiner.AddParameter(command, "@CurrentPageIndex", DbType.Int32, 0, ParameterDirection.Input, currentPageIndex);
            dataMiner.AddParameter(command, "@SortOrder", DbType.String, 500, ParameterDirection.Input, sortOrder);
            dataMiner.AddParameter(command, "@PageSize", DbType.Int32, 0, ParameterDirection.Input, pageSize);
            dataMiner.AddParameter(command, "@SearchTitle", DbType.String, 2000, ParameterDirection.Input, searchTitle);

            command.CommandText = "CSP_GetTotalGameCount";
            count = dataMiner.GetOneData(command);
            totalData = (int)CommonUtility.FilterNull(count, typeof(int));

            command.CommandText = "CSP_GetFilteredGameList";
            return dataMiner.GetListData(command);
        }
    }
}
